﻿using System;
using System.Collections.Generic;
using System.Windows.Controls;
using System.Linq;
using System.Windows;
using System.Text;



namespace QuanLyDuocPham.Controllers.QuanLyCauHinh.NguoiDung
{
    public class NHOMND_Control : IDisposable
    {
         DuocPhamDataContext db = new DuocPhamDataContext(staticClass.Conn);
         public void Dispose()
         {
             if (db != null)
             {
                 db.Dispose();
                 db = null;
             }
         }
         public void loadComboBox(ComboBox cmb)
        {

            cmb.Items.Clear();
            var types = db.NHOMND_Get_List();
            var typeorder = types.OrderBy(w => w.DIEN_GIAI);
            foreach (var t in typeorder)
            {
                cmb.Items.Add(t);
            }
            cmb.SelectedValuePath = "MA_NHOMND";
            cmb.DisplayMemberPath = "DIEN_GIAI";
            cmb.IsTextSearchEnabled = true;
            cmb.IsEditable = true;
            NHOMND_Get_ListResult temp = new NHOMND_Get_ListResult();
            
            temp.MA_NHOMND = "...";
            temp.DIEN_GIAI = "Thêm mới";
            cmb.Items.Add(temp);
        }

        public string loadID_moi()
        {
            string ID = "";

            int rowcount = db.NHOMND_Get_List().Count();
            if (rowcount == 0)
            {
                ID = "1";
            }
            else
            {
                for (int i = 1; i <= (rowcount+1); i++)
                {
                    string IDtam = i.ToString();
                    
                    
                    Boolean kq = true;
                    var data = db.NHOMND_Get_List();

                    foreach (var t in data)
                    {

                        if (t.MA_NHOMND.Trim() == IDtam.Trim())

                            kq = false;
                    }
                    if (kq == true)
                    {
                        ID = IDtam;
                        break;
                    }
                }

            }
            return ID;

        }

        public void addData(TextBox txtMa_Nhom_ND, TextBox txtDien_Giai)
        {
            if (txtDien_Giai.Text.Trim() == "")
                MessageBox.Show("Chưa nhập tên nhóm!");
            else
            {
                db.NHOMND_Insert(txtMa_Nhom_ND.Text, txtDien_Giai.Text);
                MessageBox.Show("Đã thêm thành công");
            }
        }


        public void loadData(ListView List)
        {

            var types = db.NHOMND_Get_List();
            var typeorder = types.OrderBy(w => w.MA_NHOMND);
            foreach (var t in typeorder)
            {
                List.Items.Add(t);
            }

        }

        public void updateData(TextBox txtMa_Nhom_ND, TextBox txtDien_Giai)
        {

            db.NHOMND_Update(txtMa_Nhom_ND.Text, txtMa_Nhom_ND.Text, txtDien_Giai.Text); ;
            MessageBox.Show("Đã cập nhật!");
        }

        public string deleteData(string ID_Delete, string error)
        {
            error = "";
            if (db.NHANVIEN_GetByMaNhomnd(ID_Delete).Count() > 0 || db.THUOC_QUYEN_GetByMaNhomnd(ID_Delete).Count() > 0)
            {
                error = "Không thể xóa";
            }
            else
            {
                db.NHOMND_Delete(ID_Delete);
            }
          return error;
        
        }
        public void loadForm_Edit(string ID_Edit, TextBox txtMa_Nhom_ND, TextBox txtDien_Giai)
        {

            var sl = (NHOMND_GetByMaNhomndResult)db.NHOMND_GetByMaNhomnd(ID_Edit).Single();


            txtMa_Nhom_ND.Text = sl.MA_NHOMND;
            txtDien_Giai.Text = sl.DIEN_GIAI;
        }
    }
}
